using System;
using System.Configuration;
using Microsoft.Practices.Unity.Configuration;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Microsoft.Practices.Unity.Interception
{
    [TestClass]
    public class InterceptVirtualConfigurationTest
    {
        [TestMethod]
        public void InterceptConfiguration()
        {
            Recorder.Records.Clear();
            IUnityContainer container = new UnityContainer();
            UnityConfigurationSection section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
            section.Containers.Default.Configure(container);

            SpyVirtual obj = container.Resolve<SpyVirtual>();
            obj.InterceptedMethod();
            obj.NonInterceptedMethod();

            Assert.AreEqual(4, Recorder.Records.Count);
            Assert.AreEqual("Before Method", Recorder.Records[0]);
            Assert.AreEqual("In Method", Recorder.Records[1]);
            Assert.AreEqual("After Method", Recorder.Records[2]);
            Assert.AreEqual("In Non-Intercepted Method", Recorder.Records[3]);
        }

        public class SpyVirtual
        {
            public virtual void InterceptedMethod()
            {
                Recorder.Records.Add("In Method");
            }

            public void NonInterceptedMethod()
            {
                Recorder.Records.Add("In Non-Intercepted Method");
            }

            public virtual void ThrowsException()
            {
                Recorder.Records.Add("In Method");
                throw new Exception("This is my exception!");
            }
        }
    }
}